package com.example.group_20.controller;

import com.example.group_20.entity.Students;
import com.example.group_20.mapper.StudentsMapper;
import com.example.group_20.service.Impl.StudentsServiceImpl;
import com.example.group_20.service.StudentsService;
import com.example.group_20.utils.FileUtils;
import com.example.group_20.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 栋
 * @data: 2023/8/11
 * @descriptions:
 */
@RestController
@CrossOrigin
@RequestMapping("/students")
public class StudentsController {
    @Autowired
    private StudentsService studentsService;

    @RequestMapping("login")
    public Result login(@RequestBody Map<String,String> mp){
        StringBuilder username=new StringBuilder("");
        StringBuilder pwd=new StringBuilder("");
        if(mp.containsKey("username")){
            username.append(mp.get("username"));
        }
        if(mp.containsKey("password")){
            pwd.append(mp.get("password"));
        }
        return studentsService.login(username.toString(),pwd.toString());
    }

    @RequestMapping("logout")
    public Result logout(){

        return studentsService.logout();

    }

    @RequestMapping("infor/getInfor")
    public Result getInfor(String token){
        return studentsService.getInfor(token);
    }


    @RequestMapping("infor/showInfor")
    public Result showInfor(String token){
        return studentsService.showInfor(token);
    }

    @RequestMapping("infor/updateInfor")
    public Result updateInfor(@RequestBody Map<String,Object> mp){
        StringBuilder s_id=new StringBuilder("");
        StringBuilder s_name=new StringBuilder("");
        StringBuilder s_email=new StringBuilder("");
        StringBuilder s_tel=new StringBuilder("");
        if(mp.containsKey("s_id")){
            s_id.append(mp.get("s_id"));
        }
        if(mp.containsKey("s_name")){
            s_name.append(mp.get("s_name"));
        }
        if(mp.containsKey("s_email")){
            s_email.append(mp.get("s_email"));
        }
        if(mp.containsKey("s_tel")){
            s_tel.append(mp.get("s_tel"));
        }

        return studentsService.updateInfor(new Students(s_id.toString(),s_name.toString(),null,s_email.toString(),s_tel.toString(),0,0));
    }


    @RequestMapping("infor/updatePwd")
    public Result updatePwd(@RequestBody Map<String,Object> mp){
        StringBuilder s_id=new StringBuilder("");
        StringBuilder pwd=new StringBuilder("");
        StringBuilder newpwd=new StringBuilder("");
        if(mp.containsKey("s_id")){
            s_id.append(mp.get("s_id"));
        }
        if(mp.containsKey("s_password")){
            pwd.append(mp.get("s_password"));
        }
        if(mp.containsKey("newPwd")){
            newpwd.append(mp.get("newPwd"));
        }


        return studentsService.updatePwd(new Students(s_id.toString(),null,pwd.toString(),null,null,0,0),newpwd.toString());
    }

    @RequestMapping("file/submitFile")
    public Result SubmitFile(@RequestParam("file") MultipartFile file, String s_id, String category){

        Map<String,Object>mp=new HashMap<>();
        mp.put("file",file);
        mp.put("s_id",s_id);
        mp.put("category",category);
        String path = FileUtils.getPath();
        mp.put("path",path);
        return studentsService.subFile(mp);
    }

    @RequestMapping("file/showFile")
    public Result showFile(String token){
        return studentsService.showFiles(token);
    }

    @RequestMapping("file/deleteFile")
    public Result deleteFile(@RequestBody Map<String,Object> mp){
        return studentsService.backFile(mp);
    }

    @RequestMapping("showLogInfor")
    public Result showLogInfor(String token){
        return studentsService.showSOptions(token);
    }

    @RequestMapping("sendMsg")
    public Result sendMsg(@RequestBody Map<String,Object>mp){
        return studentsService.sendMsg(mp);
    }

    @RequestMapping("showSendMsg")
    public Result showSendMsg(String token){
        return studentsService.showSendMsg(token);
    }

    @RequestMapping("showReciveMsg")
    public Result showReciveMsg(String token){
        return studentsService.showReciveMsg(token);
    }

    @RequestMapping("reply")
    public Result reply(@RequestBody Map<String,Object>mp){
        return studentsService.reply(mp);
    }

    @RequestMapping("selectGroup")
    public Result selectGroup(@RequestBody Map<String,Object>mp){
        return studentsService.selectGroup(mp);
    }
}
